<?php
/**
 * Back Order Policy 07 / 11
 * All Shop Wanted , warehouse will try best to fullfill , as long as warehouse have stock 
 * The Max Qty of Order is shop's 2 weeks consumption, shop should make sure it can consume the parts in 2 weeks
 * All Difference will be consider as back Order 
 * For One Barcode , you can only have one back order in one shop, you can not make the second backorder 
 * You can modify the back order (+/-), if you modify the Qty , the Lastest modify date will be recorded 
 * 
 * 
 * @author Norman
 *
 */

class Model_DbTable_Pr_Backorder extends Zend_Db_Table_Abstract {

	protected $_name = 'pr_products_backorder';
	
	public function getBackorder( $idBo){
		
		$row = $this->fetchRow("`id_bo` = ". $idBo);
		if(!$row) return false;
		return $row->toArray();		
		
		}
		
	public function addBackorder( $idProduct , $codeProduct ,$idShop , $qtyBo , $qtyActOrder , $qtyAllocate , $boDate , $fullfillDate , $boNo , $boStatus){
		
		$data = array(
			
         "id_product" =>  $idProduct ,
		 "code_product" => $codeProduct,		
         "id_shop" =>  $idShop ,
         "qty_bo" =>  $qtyBo ,
         "qty_act_order" =>  $qtyActOrder ,
         "qty_allocate" =>  $qtyAllocate ,
         "bo_date" =>  $boDate ,
         "fullfill_date" =>  $fullfillDate ,
         "bo_no" =>  $boNo ,
         "bo_status" =>  $boStatus 
	
			);
		$this->insert($data);
		
		}
		
	public function updateBackorder(  $idBo ,  $idProduct ,  $codeProduct ,$idShop , $qtyBo , $qtyActOrder , $qtyAllocate , $boDate , $fullfillDate , $boNo , $boStatus){
		$data = array(
			
         "id_product" =>  $idProduct ,
				"code_product" => $codeProduct,
         "id_shop" =>  $idShop ,
         "qty_bo" =>  $qtyBo ,
         "qty_act_order" =>  $qtyActOrder ,
         "qty_allocate" =>  $qtyAllocate ,
         "bo_date" =>  $boDate ,
         "fullfill_date" =>  $fullfillDate ,
         "bo_no" =>  $boNo ,
         "bo_status" =>  $boStatus 
	
			);
			
		$this->update($data,"`id_bo` = ". $idBo);
		}
	public function updateBoQty($idBo,$qtyBo){
		$data = array(
				"qty_bo" =>  $qtyBo
		);
			
		$this->update($data,"`id_bo` = ". $idBo);
		
	}	
		
	public function deleteBackorder( $idBo){
		
		$this->delete("`id_bo` = ". $idBo);
		
		}	
	
	public function listAll(){
		
		$rows =$this->fetchAll("1","id_bo DESC ");
		if(!$rows) return false;
		return $rows->toArray();			
		
		}	
	public function listAllNotFullfilled(){
		
		$rows = $this->fetchAll("`fullfill_date` IS NULL","code_product");
		if(!(boolean)$rows->toArray()) return false;
		return $rows->toArray();
		
	}

	public function getNotFullfilledBo($codeProduct,$idShop){
		
		$rows = $this->fetchRow("`fullfill_date` IS NULL AND `code_product` LIKE '{$codeProduct}' AND `id_shop` = {$idShop}");
		if(!$rows) return false;
		$row = $rows->toArray();
		return $row['qty_bo'];
	}
	
	public function getLastNotFullfilledBo($codeProduct,$idShop){
	
		$rows = $this->fetchRow("`fullfill_date` IS NULL AND `code_product` LIKE '{$codeProduct}' AND `id_shop` = {$idShop}");
		if(!$rows) return false;
		$row = $rows->toArray();
		return $row;
	}
	
	public function getAllNotFullfilledBo($codeProduct,$idShop){
		$rows = $this->fetchAll("`fullfill_date` IS NULL AND `code_product` LIKE '{$codeProduct}' AND `id_shop` = {$idShop}","bo_date");
		if(!(boolean)$rows->toArray()) return false;
		return $rows->toArray();
	}
	
	public function getBoByDate($dateBo){
		
		$rows = $this->fetchAll("`bo_date` LIKE '{$dateBo}'","code_product");
		if(!(boolean)$rows->toArray()) return false;
		return $rows->toArray();
		
	}
	public function fullfillBackOrder($idBo,$fullfillDate){
		
		$data = array(
				"fullfill_date" =>  $fullfillDate
				);
				$this->update($data,"`id_bo` = ". $idBo);
	}
	
	public function boPartsList($idShop){
		$rows = $this->fetchAll("`id_shop` = {$idShop}","code_product");
		if(!(boolean)$rows->toArray()) return false;
		
		$arrParts = array();
		
		foreach($rows->toArray() as $row){
				$arrParts[] = $row['code_product'];
		}
		return array_unique($arrParts);
	}
}


?>